<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\index\controller;

use think\admin\Controller;

/**
 * Class Index
 * @package app\index\controller
 */
class Index extends Controller
{
    public function index()
    {
        //查询会议室
        $this->room = $this->app->db->name('company_room')->where(['status'=>1])->select()->toArray();
        $this->fetch();
    }

    public function upAppointment()
    {
        $data = input();
        $timeArr = explode('-',$data['time']);
        $star_time = strtotime($data['date']." ".$timeArr[0]);
        $end_time = strtotime($data['date']." ".$timeArr[1]);
        if($star_time>$end_time){
            $this->error("预约开始时间大于结束时间,请核对");
        }
        //dump($star_time);
        //dump($end_time);
        $info = $this->app->db->name('company_room_appointment')->where(['room_id'=>$data['room_id']])
            ->whereRaw("(star_time <= $star_time and end_time > $star_time) or (star_time < $end_time and end_time >= $end_time)")
            ->select()->toArray();
       // dump($info);
        if($info){
            $this->error("该时间已被预约,请更改时间");
        }
        $saveData=[
            'room_id'=>$data['room_id'],
            'star_time'=>$star_time,
            'end_time' => $end_time,
            'username' =>$data['username'],
            'dep_name' =>$data['dep_name'],
            'remake' =>$data['remake'],
            'phone' =>$data['phone'],
            'title' => $data['title']
        ];
        $this->app->db->name('company_room_appointment')->save($saveData);
        $this->success("预约成功");
    }

    public function show()
    {
        $data = $this->app->db->name('company_room_appointment')
            ->where(['status'=>1])->select()->toArray();
        $room = $this->app->db->name('company_room')->where(['status'=>1])->select()->toArray();
        $roomNew=[];
        foreach ($room as $v){
            $roomNew[$v['id']]=$v;
        }
        foreach ($data as $k=>$v){
            if(isset($roomNew[$v['room_id']])){
                $data[$k]['room']=$roomNew[$v['room_id']]['name'];
            }
        }
        $this->appointment=$data;
       // dump($appointment);
        $this->fetch();
    }
}